package com.sduept.nwld.secondaryequipment.evaluation;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.dataserver.manager.protection.EquipmentTripDetailManager;
import com.sduept.nwld.dataserver.model.daily.Tripping;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Named
@ViewScoped
public class LineDataController extends AbstractController {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7694695030376170067L;
	@Autowired
	private EquipmentTripDetailManager etdm;
	@Autowired
	private CimManager cm;
	private Date startDate;
	private Date endDate;
	private String lineId;
	private String lineName;
	
	private List<Tripping> records = new ArrayList<>();
	
	@PostConstruct
	public void init(){
		lineId = getParameter("id");
		lineName =  cm.getAcLineSegmentById(lineId).getName();
		if(lineName != null){
			endDate = new Date();
			DateTime dt = new DateTime(endDate.getTime());
			
			startDate = dt.minusMonths(1).toDate();
			records =etdm.listTrippings(lineName);
		}
	}

	
	public void query(){
		records.clear();
		if(startDate != null && endDate != null && endDate.getTime() > startDate.getTime()){
			records = etdm.listTrippings(lineName, startDate, endDate);
		} else {
			records =etdm.listTrippings(lineName);
		}
	}
	
	public void clearQuery(){
		records.clear();
		startDate = null;
		endDate = null;
	}
	
	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public List<Tripping> getRecords() {
		return records;
	}

	public void setRecords(List<Tripping> records) {
		this.records = records;
	}


	public String getLineName() {
		return lineName;
	}


	public void setLineName(String lineName) {
		this.lineName = lineName;
	}

}
